package com.tanhua.server.controller;

import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RequestMapping("/user")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;


    /**
     * 获取登录验证码
     * 请求参数：phone （Map）
     * 响应：void
     * ResponseEntity
     */
    @PostMapping("/login")  //@RequestBody Map map
    public ResponseEntity login(@RequestBody Map map) throws Exception {
        String phone = (String) map.get("phone");
        //String phone = "13751381735";
        userService.sendMsg(phone);
        // ResponseEntity.status(500).body("出错啦");
        return ResponseEntity.ok(null); //正常返回状态码200
    }

    /**
     * 检验登录
     * 1.传入手机号
     * 2.传入输入的验证码
     * 通过loginVerification(phone,code)方法验证验证码是否正确和用户是否是新用户
     * 新用户则创建用户并跳转到个人信息页面 老用户则跳转到主页面
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
            //1、调用map集合获取请求参数
            String phone = (String) map.get("phone");
            String code = (String) map.get("verificationCode");
            //2、调用userService完成用户登录
            Map retMap = userService.loginVerification(phone, code);
            //3、构造返回值
            return ResponseEntity.ok(retMap);
    }


    /*@PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto, @RequestHeader("Authorization") String token) {
        return null;
    }*/
}
